// packagBuy/pages/marketing/verInforCode/verInforCode.js
import { couponApi } from '../../../../../config/config'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusHeight,
    name: '',
    phone:'',
    newPhone: '',
    couponId: '',
    veCode: new Array(),
    time: 60,
    inputLen: 6,
    iptValue: "",
    isFocus: true,
    reqLoading:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    if (options) {
      let name = options.name
      let phone = options.phone
      let couponId = options.couponId
      let newPhone = phone.substr(0,3) + "****" + phone.substr(7)
      that.setData({
        name: name,
        phone: phone,
        newPhone: newPhone,
        couponId: couponId
      })
      that.getCode(phone)
    }
    that.againTime()
  },
  goBacke() {
    wx.navigateBack({
      delta: 1,
    })
  },
  getCode(phone, again) {
    app.reqFun.requestJson('POST',  couponApi.sendSmsCode+'?phone=' + phone, '', (response) => {
      const res=response.data
      if (res.success == 1) {
        if (again) {
          this.setData({ time: 60 })
          this.againTime()
        }
      }else{
        wx.showToast({
          title:res.msg || '获取验证码失败',
          icon: 'none',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
        })
      }
    })
  },
  // 倒计时
  againTime() {
    let time = this.data.time;
    clearInterval(timing);
    let timing = setInterval(() => {
      if (time <= 0) {
        clearInterval(timing)
      } else {
        time--;
        this.setData({
          time: time
        })
      }
    }, 1000)
  },
  againTimeBtn() {
    this.setData({ isFocus: true })
    this.getCode(this.data.phone, 1)
  },


  onFocus: function (e) {
    let that = this
    that.setData({ isFocus: true })
  },
  setValue: function (e) {
    let that = this
    that.setData({ iptValue: e.detail.value })
    if (e.detail.value.length === 6) {
      that.validate(that.data.name, that.data.phone, that.data.iptValue, that.data.couponId)
    }
  },
  //  优惠卷-短信验证
  validate(name, phone, smsCode, couponId) {
    if(this.data.reqLoading) return
    let receivePage = wx.getStorageSync('receivePage')
    let params = {
      name: name,
      phone: phone,
      smsCode: smsCode,
      couponId: couponId,
      receivePage: receivePage
    }
    this.setData({reqLoading:true})
    app.reqFun.requestJson('POST',  couponApi.validate, params, (response) => {
      const res=response.data
      if (res.success == 1) {
        this.setData({ isFocus: false,reqLoading:false })
        wx.navigateTo({
          url: '/packagBuy/pages/marketing/verifyCoupon/successCoupon/successCoupon',
        })
      }else{
        wx.showToast({
          title:res.msg || '领券失败',
          icon: 'none',   // 图标类型，默认success
          duration: 1500   // 提示窗停留时间，默认1500ms
        })
        this.setData({reqLoading:false})
      }
    }).catch(err=>{
      this.setData({reqLoading:true})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 禁用用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})